package com.tanhua.management.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.Map;


/**
 * @Author: PeiLin·Wang
 * @Description: 类说明
 * @Date: Created in 16:00 2021/1/17
 * @ModifiedBy:
 */
@Service
public class UserFreezeService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    private static final String FREEZE_PREFIX="FREEZE_";
    /**
     * 用户冻结操作
     *
     * @param
     * @return
     */
    public Boolean freeze(Map<String, Object> param) {
        //获取用户id
        Integer userId = (Integer) param.get("userId");
        String key=FREEZE_PREFIX+userId;
        Boolean s = this.redisTemplate.hasKey(key);
        if(!s){

            Integer freezingTime = Integer.parseInt(param.get("freezingTime").toString());
            //判断时间
            if(freezingTime==1){
                this.redisTemplate.opsForValue().set(key,"ok", Duration.ofDays(3));
            }else if(freezingTime ==2){
                this.redisTemplate.opsForValue().set(key,"ok", Duration.ofDays(7));
            }else{
                this.redisTemplate.opsForValue().set(key,"ok");
            }

            return true;
        }

        return false;
    }

    /**
     * 用户解冻操作
     *
     * @param param
     * @return
     */
    public Boolean unfreeze(Map<String, Object> param) {
        Integer userId = Integer.parseInt(param.get("userId").toString());
        String key=FREEZE_PREFIX+userId;
        String s = this.redisTemplate.opsForValue().get(key);
        if(null != s){
            this.redisTemplate.delete(key);
            return true;
        }
        return false;
    }
}
